package com.sunny.ynhz.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;

import com.amap.api.maps.model.LatLng;

/**
 * Created by Administrator on 2017/7/17/017.
 */

public class LocationGpsUtils {
    static LocationManager locationManager;
    static boolean isLocation = false;
    static IlocationCall mIlocationCall;
    static Location location;
    static Activity activity;

    public interface IlocationCall {
        public void locationSuccess(LatLng latLng);
    }

    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                mIlocationCall.locationSuccess(new LatLng(location.getLatitude(), location.getLongitude()));
            }
            handler.sendEmptyMessageDelayed(0, 10000);

        }
    };

    /**
     * 定位的状态
     */
    static LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
//            mIlocationCall.locationSuccess(new LatLng(location.getLatitude(), location.getLongitude()));

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };


    //状态监听
    static GpsStatus.Listener listener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            switch (event) {
                //第一次定位
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    isLocation = true;
                    break;
                //卫星状态改变
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    break;
                //定位启动
                case GpsStatus.GPS_EVENT_STARTED:
                    break;
                //定位结束
                case GpsStatus.GPS_EVENT_STOPPED:
                    break;
            }
        }

        ;
    };

    public static LocationManager getInstanceLocationManager(Context context, IlocationCall ilocationCall) {
        if (locationManager == null) {
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            mIlocationCall = ilocationCall;
        }
        return locationManager;
    }

    public static void isOpenGps(Activity mactivity) {
        activity = mactivity;
        boolean providerEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!providerEnabled) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mactivity.startActivityForResult(intent, 0);

        }
    }

    public static void startLocation() {
        String bestProvider = locationManager.getBestProvider(getCriteria(), true);
        location = locationManager.getLastKnownLocation(bestProvider);
        locationManager.addGpsStatusListener(listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, locationListener);
        handler.sendEmptyMessageDelayed(0, 10000);

    }

    public static void stopLocation() {
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }


    /**
     * 返回查询条件
     *
     * @return
     */
    private static Criteria getCriteria() {
        Criteria criteria = new Criteria();
        //设置定位精确度 Criteria.ACCURACY_COARSE比较粗略，Criteria.ACCURACY_FINE则比较精细
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //设置是否要求速度
        criteria.setSpeedRequired(false);
        // 设置是否允许运营商收费
        criteria.setCostAllowed(false);
        //设置是否需要方位信息
        criteria.setBearingRequired(false);
        //设置是否需要海拔信息
        criteria.setAltitudeRequired(false);
        // 设置对电源的需求
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        return criteria;
    }

    public static void clearhandler() {
        handler.removeMessages(0);
    }


}
